package net.zjitc.controller;

import net.zjitc.dao.BillDao;
import net.zjitc.pojo.Bill;
import net.zjitc.pojo.BillItem;
import net.zjitc.pojo.BillResponse;
import net.zjitc.pojo.Customer;
import net.zjitc.service.DiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
public class DiscountController {
    @Autowired
    private DiscountService discountService;

    @Autowired
    private BillDao billDao;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @GetMapping("/getCustomer")
    public Customer getCustomer(@RequestParam String idCard){
        return discountService.getCustomer(idCard);
    }

    @GetMapping(value = "/getVipLevel",
            consumes = MediaType.APPLICATION_ATOM_XML_VALUE,
            produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    public String getVipDemo(@RequestParam String idCard){
        //获取customer的信息
        //判断vip信息
        Customer customer=discountService.getCustomer(idCard);
        if (customer.getVipType() == null) {
            return "not vip";
        }else {
            return customer.getVipType();
        }
    }

    @GetMapping("/discount")
    public String getDiscount(@RequestParam String itemName,
                              @RequestParam BigDecimal price,
                              @RequestParam String vipLevel) throws Exception {
        BigDecimal currentPrice=discountService.discount(vipLevel, itemName, price);
        return currentPrice.toString();
    }

    @RequestMapping("/bills")
    public List<Bill> getBills(@RequestParam Long customerId) {
        return  discountService.findByCustomerId(customerId);
    }

    @PostMapping("/bill")
    public Bill postBill(@RequestBody Bill bill) throws Exception {
//        System.out.println(bill);
//        long customerId=bill.getCustomerId();
//        Customer customer=discountService.getCustomerById(customerId);
//        String vipType=customer.getVipType();
//        BigDecimal totalPrice=BigDecimal.valueOf(0);
//        for (BillItem billItem:bill.getBillItemList()){
//            BigDecimal discountPrice=discountService.discount(vipType,billItem.getItemName(),billItem.getPrice());
//            billItem.setDiscountPrice(discountPrice);
//            totalPrice=totalPrice.add(discountPrice);
//        }
//
//
//        return bill;

        //获取用户信息
        Long customerId=bill.getCustomerId();
        Customer customer=discountService.getCustomerById(customerId);

        //获取优惠信息
        BigDecimal totalPrice=BigDecimal.valueOf(0);
        for (BillItem billItem:bill.getBillItemList()){
            BigDecimal discountPrice=discountService.discount(customer.getVipType(),
                                            billItem.getItemName(),billItem.getPrice());
            billItem.setDiscountPrice(discountPrice);
            totalPrice.add(discountPrice);
        }
        //todo:保存bill数据
        bill.setTotalPrice(totalPrice);
        discountService.updateBillTotalPrice(bill);
        //返回bill
        return bill;
    }


    @PostMapping("/calculate_price")
    public BillResponse calculatePrice(@RequestBody Bill bill) throws Exception {
        Long customerId =bill.getCustomerId();
        Customer customer=discountService.getCustomerById(customerId);
        String vipType=customer.getVipType();
        BigDecimal totalPrice= BigDecimal.valueOf(0);
        for (BillItem billItem:bill.getBillItemList()){
            BigDecimal discount=billItem.getPrice().subtract(billItem.getDiscountPrice());
            totalPrice=totalPrice.add(discount);
        }
        bill.setTotalPrice(totalPrice);
        BillResponse billResponse=BillResponse.convertFromBill(bill,customer.getName(),customer.getIdCard());
        return billResponse;
    }



}
